<?php

namespace iBrand\Shop\Core\Models;

use iBrand\Component\Order\Models\Order as ComponentOrder;

class Order extends ComponentOrder
{
	const SPOT_PICK_UP = '现场提货';

	public function shopOrder()
	{
		return $this->hasOne(ShopOrder::class);
	}

	public function orderRefund()
	{
		return $this->hasOne(OrderRefundRelation::class);
	}

	public function getOrderUserNameAttribute()
	{
		$user = $this->user;
		if ($user) {
			if ($user->name) {
				return $user->name;
			}
			if ($user->mobile) {
				return $user->mobile;
			}
			if ($user->nick_name) {
				return $user->nick_name;
			}
		}

		return '/';
	}

	/**
	 * 订单状态
	 *
	 * @return string
	 */
	public function getStatusTextAttribute()
	{
		$text       = '';
		$isSupplier = session('admin_check_supplier');

		if ($this->distribution_status == 2 AND !$isSupplier) {
			return '部分已发货';
		}

		if ($isSupplier AND $this->pay_status == 1) {
			$supplierID = session('admin_supplier_id');
			$items      = $this->items->filter(function ($value, $key) use ($supplierID) {
				return in_array($value->supplier_id, $supplierID);
			});

			return $items->first()->is_send ? '已发货' : '待发货';
		}

		switch ($this->status) {
			case 1:
				$text = "待付款";
				break;
			case 2:
				$text = "待发货";
				break;
			case 3:
				$text = "配送中";  //或 待收货
				break;
			case 4:
				$text = "待评价";  //已收货待评价
				break;
			case 5:
				$text = "已完成";
				break;
			case 6:
				$text = "已取消";
				break;
			case 7:
				$text = "退款中";
				break;
			case 8:
				$text = "已作废";
				break;
			case 9:
				$text = "已删除";
				break;
			default:
				$text = "待付款";
		}

		return $text;
	}
}

